package src.model.gameObject;

import java.util.Vector;

import src.model.gameObject.ability.AbilityReturnResult;
import src.model.gameObject.ability.AbilityType;
import src.model.gameObject.ability.ActiveAbility;
import src.model.gameWorld.Position;

public class GameObjectEventHandler{
	private static Vector<Vector<ActiveAbility>> abilities;
	private static Vector<PlayerGameObject> selectedGameObjects;

	public GameObjectEventHandler(){
		abilities = new Vector<Vector<ActiveAbility>>();
		selectedGameObjects = new Vector<PlayerGameObject>();
	}

	public static AbilityReturnResult useAbility(int abilityID){
		if(abilities == null)
			return AbilityReturnResult.IMPOSSIBLE;

		if(abilityID < 0 || abilityID > abilities.size())
			return AbilityReturnResult.IMPOSSIBLE;

		AbilityReturnResult arr = AbilityReturnResult.SUCCESS;

		for(Vector<ActiveAbility> abilVector : abilities){
			for(ActiveAbility ability : abilVector){
				if(!ability.queueToMission())
					arr = AbilityReturnResult.NEEDSMOREINFORMATION;
			}
		}
		return arr;
	}

	public static AbilityReturnResult useAbility(int abilityID, Position tile){
		if(abilities == null)
			return AbilityReturnResult.IMPOSSIBLE;

		if(abilityID < 0 || abilityID > abilities.size())
			return AbilityReturnResult.IMPOSSIBLE;

		for(Vector<ActiveAbility> abilVector : abilities){
			for(ActiveAbility ability : abilVector){
				ability.queueToMission(tile);
			}
		}

		return AbilityReturnResult.SUCCESS;
	}

	public static AbilityReturnResult useMoveAbility(Position tile){
		boolean canMove = false;
		int abilIndex = -1;
		int countAbilIndex = 0;
		for(Vector<ActiveAbility> abilVector : abilities){
			if(abilVector.size() > 0){
				if(abilVector.get(0).abilityType() == AbilityType.MOVE){
					abilIndex = countAbilIndex;
					canMove = true;
				}
			}
			countAbilIndex++;
		}

		if(!canMove)
			return AbilityReturnResult.IMPOSSIBLE;

		return useAbility(abilIndex, tile);
	}


	public static void clearQueue(){
		for(PlayerGameObject pog : selectedGameObjects){
			pog.clearAbilityQueue();
		}
	}

	//This gets called whenever the player unselects units; i.e. to go into cursor mode
	public static void removeSelectedUnits(){
		selectedGameObjects = new Vector<PlayerGameObject>();
		abilities = new Vector<Vector<ActiveAbility>>();
	}

	//This gets called whenever a unit selection happens
	public static void swapSelectedUnits(Vector<PlayerGameObject> selectedGameObjects){
		removeSelectedUnits();

		GameObjectEventHandler.selectedGameObjects = selectedGameObjects;

		if(selectedGameObjects.size() <= 0)
			return;

		Vector<AbilityType> firstObjectsAbilList = selectedGameObjects.get(0).getActiveAbilityTypeList();

		Vector<ActiveAbility> tempAbilList = new Vector<ActiveAbility>();

		boolean abilityFucked;

		for(AbilityType abilType : firstObjectsAbilList){
			abilityFucked = false;

			tempAbilList = new Vector<ActiveAbility>();

			for(PlayerGameObject pog : selectedGameObjects){
				if(!pog.hasAbility(abilType))
					abilityFucked = true;
				else
					tempAbilList.add(pog.getActiveAbility(abilType));
			}

			if(!abilityFucked)
				abilities.add(tempAbilList);
		}
	}

	public static void addSelectedUnits(Vector<PlayerGameObject> addedGameObjects){

	}

	//These 2 methods are mainly called for toggelable abilities
	public static void removeAbility(AbilityType abilType){
		abilities.remove(abilType);
	}

}
